package com.hele.sts.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.hele.sts.pub.ConnectDB;
import com.hele.sts.pub.SpringContextHolder;

public class QPrintLogDao {
	static Logger logger = LoggerFactory.getLogger(QPrintLogDao.class);
	public static final String TABLE = "da_q_printlog";

	public static boolean insert(QPrintLog bean) {
		Connection conn = null;
		PreparedStatement pstmt = null;
		String sql = null;
		ConnectDB connsyb = null;

		sql = "INSERT INTO da_q_printlog (logid,userid,nbxh,jnmlbh,inmlbh,inmlmc,days,sfdy,djid,dyrq) "
				+ "select ?,?,nbxh,jnmlbh,t.inmlbh,clmlmc as inmlmc,?,'1',?,sysdate from da_qyjnmlb t where nbxh=? and jnmlbh=? and inmlbh=?";

		logger.debug(sql);
		try {
			connsyb = SpringContextHolder.getBean(ConnectDB.class);
			conn = connsyb.getConnection();
			pstmt = conn.prepareStatement(sql);
			int i = 1;
			pstmt.setString(i++, bean.getLogId());

			pstmt.setString(i++, bean.getUserId());
			pstmt.setInt(i++, bean.getDays());
			pstmt.setString(i++, bean.getDjid());
			pstmt.setString(i++, bean.getNbxh());

			pstmt.setInt(i++, bean.getJnmlbh());

			pstmt.setInt(i++, bean.getInmlbh());
			
			pstmt.execute();
			return true;
		} catch (SQLException e) {
			logger.error("db error", e);
		} finally {
			DbClose.freePreparedStatement(pstmt);
			DbClose.freeConnection(conn);
		}
		return false;
	}

	public static QPrintLog query(String userId, String nbxh, String djid, int jnmlbh, int inmlbh) {
		Connection conn = null;
		PreparedStatement pstmt = null;
		String sql = null;
		ConnectDB connsyb = null;
		ResultSet rs = null;
		QPrintLog log = null;
		sql = "select * from da_q_printlog where userid=? and nbxh=? and djid=? and jnmlbh=? and inmlbh=? ";

		logger.debug(sql);
		try {
			connsyb = SpringContextHolder.getBean(ConnectDB.class);
			conn = connsyb.getConnection();
			pstmt = conn.prepareStatement(sql);
			int i = 1;

			pstmt.setString(i++, userId);
			pstmt.setString(i++, nbxh);
			pstmt.setString(i++, djid);
			

			pstmt.setInt(i++, jnmlbh);

			pstmt.setInt(i++, inmlbh);
			
			rs = pstmt.executeQuery();
			if (rs.next()) {
				log = rsToBean(rs);
		      }
			return log;
		} catch (SQLException e) {
			logger.error("db error", e);
		} finally {
			DbClose.freePreparedStatement(pstmt);
			DbClose.freeConnection(conn);
		}
		return null;
	}

	private static QPrintLog rsToBean(ResultSet rs) throws SQLException {
		QPrintLog log = new QPrintLog();
		log.setLogId(rs.getString("logid"));
		log.setUserId(rs.getString("userid"));
		log.setDjid(rs.getString("djid"));
		log.setNbxh(rs.getString("nbxh"));
		log.setJnmlbh(rs.getInt("jnmlbh"));
		log.setInmlbh(rs.getInt("inmlbh"));
		log.setInmlmc(rs.getString("inmlmc"));
		log.setSfdy(rs.getString("sfdy"));
		log.setDays(rs.getInt("days"));
		log.setDyrq(rs.getTimestamp("dyrq"));
		return log;
	}
}
